//
//  SettingTableViewControll.m
//  LvXi_newVersion
//
//  Created by 王敏 on 16/8/10.
//  Copyright © 2016年 SJT. All rights reserved.
//

#import "SettingTableViewController.h"

#import "MallSetingCell.h"

@interface SettingTableViewController (){
    UIImageView * _screenShort;
  
}

@property(nonatomic,strong)NSArray * sectionHeaders;

@property(nonatomic,strong)NSArray * options;

@end

@implementation SettingTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.sectionHeaderHeight = 0;
    self.tableView.sectionFooterHeight = 0;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self xw_registerBackInteractiveTransitionWithDirection:XWInteractiveTransitionGestureDirectionRight transitonBlock:^(CGPoint startPoint){
        [self.navigationController popViewControllerAnimated:YES];
    } edgeSpacing:100];
    self.navigationItem.title = @"设置";
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self setUpNavi];
    self.tableView.y = 64;
    [_screenShort removeFromSuperview];
    self.tableView.contentInset = (UIEdgeInsets){0,0,0,0};
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [_screenShort removeFromSuperview];
    _screenShort = [[UIImageView alloc] initWithImage:[LXMallFrame screenShotWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)]];
    _screenShort.y = -64;
    [self.view addSubview:_screenShort];
}

#pragma mark - 设置导航栏
-(void)setUpNavi{
    UINavigationBar * navigationBar = self.navigationController.navigationBar;
    navigationBar.hidden = NO;
    navigationBar.translucent = NO;
    self.view.y = -64;
    self.navigationItem.title = @"设置";
    [navigationBar setBackgroundImage:[UIImage imageWithColor:HexColor(@"#3CB034") size:CGSizeMake(ScreenWidth, 64)] forBarMetrics:UIBarMetricsDefault];
    [navigationBar setShadowImage:[UIImage imageWithColor:[UIColor clearColor] size:CGSizeMake(ScreenWidth, 1)]];
    
    
    UIButton * backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(0, 0, 20, 20);
    [backButton setImage:[UIImage imageNamed:@"nav_return"] forState:UIControlStateNormal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    [backButton addTarget:self action:@selector(backButtonClick:) forControlEvents:UIControlEventTouchUpInside];
}


-(void)backButtonClick:(UIButton*)button{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.sectionHeaders.count;
}


-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * cellID = @"dfgdfgufg";
    MallSetingCell * cell;
    cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if(cell  == nil){
        cell = [[MallSetingCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    cell.backgroungImageView.transform = CGAffineTransformIdentity;
    [cell.backgroungImageView addRadius:0];
    cell.textLabel.text = self.options[indexPath.section][indexPath.row];
    cell.textLabel.font = [UIFont systemFontOfSize:ISBigScreen?15:13];
    
    cell.backGroundImage = [UIImage imageWithColor:[UIColor whiteColor] size:CGSizeMake(ScreenWidth, cell.cellHeight)];
    cell.enableUnderLine = YES;
    
    if(indexPath.section == 0){
        if(indexPath.row == 0){
             cell.enableUnderLine = NO;
            UISwitch * pushSwitch = [[UISwitch alloc] init];
            [pushSwitch addTarget:self action:@selector(pushSwitchStateChange:) forControlEvents:UIControlEventValueChanged];
            pushSwitch.on = PushState;
            cell.accessoryView = pushSwitch;
            [cell.backgroungImageView addRadius:10];
        }
    }else{
        if(indexPath.row == 0){
            cell.backGroundImage = [UIImage imageNamed:@"brokerage_bg02"];
            cell.backgroungImageView.transform = CGAffineTransformRotate(cell.backgroungImageView.transform, M_PI);
        }
        if(indexPath.row==1){
            CGFloat mb =  [LXDiskManager getCacheSize];
            cell.detailTextLabel.text = [NSString stringWithFormat:@"%.2fMB",mb];
        }
        
        if(indexPath.row == [self.options[1] count]-1){
            cell.backGroundImage = [UIImage imageNamed:@"brokerage_bg02"];
            cell.enableUnderLine = NO;
//            cell.backgroungImageView.transform = CGAffineTransformRotate(cell.backgroungImageView.transform, M_PI);
        }
        
    
    }
    return cell;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.options[section] count];
}

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView * containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,ScreenWidth , 30)];
    
    UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(16, 0,ScreenWidth - 16 , 30)];
    [containerView addSubview:label];
    label.text = self.sectionHeaders[section];
    label.font = [UIFont systemFontOfSize:ISBigScreen?12:10];
    return containerView;
}


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section==1&&indexPath.row == 1){
        CGFloat mb =  [LXDiskManager getCacheSize];
        [self.view alertWithTitle:@"" mesege:[NSString stringWithFormat:@"将要清除%.2fMB的缓存",mb] buttons:@[@"👌",@"不要"] handle:^(NSInteger buttonIndex) {
            if(buttonIndex==0){
                [LXDiskManager cleanCache:^{
                    [self.tableView reloadData];
                   [self.view alertWithTitle:@"" mesege:@"完成" buttons:@[@"知道了"] handle:^(NSInteger buttonIndex) {
                       
                   }];
                }];
            }
        }];
    }
}

-(NSArray*)sectionHeaders{
    if(_sectionHeaders == nil){
        _sectionHeaders = @[@"推送设置",@"其他"];
    }
    return _sectionHeaders;
}


-(NSArray*)options{
    if(_options == nil){
        _options = @[@[@"推送开关"],@[@"评分",@"清除缓存",@"关于我们"]];
    }
    return _options;
}



-(void)pushSwitchStateChange:(UISwitch*)sw{
    SetPushState(sw.isOn);
}
-(void)viewDidLayoutSubviews
{
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.tableView setLayoutMargins:UIEdgeInsetsZero];
    }
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

@end
